
/**
 * 获取导航栏数据
 *      从cookie中读取导航数据
 */
function getNavigationList(){
    var navigationData = $.cookie("navigationData");
    if(navigationData == null){//cookie中没有导航数据，则从后台调用
        ajaxRequest("get", $("input[name='navigationListUrl']").val(), null, true, "navigationCallback");
    }else{//从cookie中读取导航数据进行处理
        navigationCallback(navigationData);
    }
}

/**
 * 导航数据回调 class=\"current-menu-item\"
 */
function navigationCallback(data){
    var result;//最终的导航数据
    
    var navigationData = $.cookie("navigationData");
    if(navigationData == null){
        result = JSON.parse(data);
        
        //设置cookie有效期为1天
        var date = new Date();
        date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
        //将导航数据放在客户端cookie中
        $.cookie("navigationData", data, {path: '/', expires: date});
    }else{
        result = JSON.parse($.cookie("navigationData"));
    }
    //需要将返回的字符串结果，先转换成json格式对象
    var dataArr = result;
    for (var i = 0; i < dataArr[0].length; i++) {
        var str = "<li>";
        // 子菜单数量
        var subLength = dataArr[0][i].sub.length;
        if (subLength <= 0) {// 没有子菜单选项
            if (dataArr[0][i].parent.linkType == 2) {// 跳转到view
                str += '<a href="' + $("input[name='projectUrl']").val() + dataArr[0][i].parent.link + '">' + dataArr[0][i].parent.name + "</a>";
            } else {// 查询该模块下的文章列表
                str += "<a href='" + $("input[name='articleListUrl']").val() + "?navigationId=" + dataArr[0][i].parent.id + "'>" + dataArr[0][i].parent.name + "</a>";
            }
        } else {
            str += '<a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">' + dataArr[0][i].parent.name + '<span class="caret"></span></a>';
            str += '<ul class="dropdown-menu" role="menu">';
            for (var k = 0; k < subLength; k++) {
                if(dataArr[0][i].sub[k].linkType == 1){//非视图view
                    str += "<li><a href='" + $("input[name='articleListUrl']").val() + "?navigationId=" + dataArr[0][i].sub[k].id + "'>" + dataArr[0][i].sub[k].name + "</a></li>";
                }else{
                    str += "<li><a href='" + $("input[name='projectUrl']").val() + dataArr[0][i].sub[k].link + "'>" + dataArr[0][i].sub[k].name + "</a></li>";
                }
                if (k < subLength - 1) {
                    str += '<li class="divider"></li>';
                }
            }
            str += "</ul>";
        }
        str += "</li>";

        $(".navbar-nav").append(str);
    }
}

/**
 * 初始化
 */
$(document).ready(function() {
    getNavigationList();
});